# coding = utf-8
import requests # 导入请求模块
import re  # 导入正则匹配模块
#  百思不得妹全站视频下载
# 1.页面请求并采集返回内容
def get_html(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/49.0.2623.75 Safari/537.36'}
    html = requests.get(url, headers=headers).text
    return html

# 2.匹配html中数据
def html_part(html):
    ret= re.compile(r'(<div class="j-r-list-c">.*?</div>.*?</div>)',re.S)
    return re.findall(ret,html)

# 2.1 匹配MP4数据
def mp4_url(response):
    ret = r'data-mp4="(.*?)"'
    return str(re.findall(ret,response))[2:-2]

# 2.2 匹配mp4数据名称
def mp4_name(response):
    ret = re.compile('<a href="/detail-(.*?)</a>')
    return str(re.findall(ret,response))[17:-2]

# 3. 下载地址记录html
def download_url(contents):
    log = open(r"D:\video\log.html","a+",encoding='utf-8')   # 视频下载链接log.html并存在D盘video目录
    log.write(contents)
    log.close()

# 4. 视频下载
def download_video(mp4_url,mp4_name,num):
    video_con = requests.get(mp4_url)
    with open(r"D:\video\%d %s.mp4"%(num,mp4_name), "wb") as code:   # 文件保存在D盘video文件夹内
        code.write(video_con.content)

if __name__ == '__main__':
    times = 50
    times2 = 1
    html_head = \
        '<!DOCTYPE html>'+'\n'\
        +'<html lang="en">'+'\n'\
        +'<head>'+'\n'\
        +'    <meta charset="UTF-8">'+'\n'\
        +'    <meta name="viewport" content="width=device-width, initial-scale=1.0">'+'\n'\
        +'    <meta http-equiv="X-UA-Compatible" content="ie=edge">'+'\n'\
        +'    <title>百思不得妹视频链接记录</title>'+'\n'+'    <style>'+'\n'+'    *{'+'\n'\
        +'        text-decoration: none;'+'\n'+'        color: black;'+'\n'+'    }'+'\n'+'    </style>'+'\n'\
        +'</head>'+'\n'+'<body>'+'\n'
    html_tail=\
        '</body>'+'\n'\
        +'</html>'
    download_url(html_head)
    while times > 0:
        url = 'http://www.bude***.com/video/'+str(times)  #  此处链接做处理防止侵权
        connect = html_part(get_html(url))
        for i in connect:
            print (str(times2)+'. '+mp4_name(i)+'  网址:'+mp4_url(i))
            download_url('<a href="'+mp4_url(i)+'">'+str(times2)+'. '+mp4_name(i)+'</a>'+'<br>'+'\n')
            download_video(mp4_url(i),mp4_name(i),times2)
            times2 += 1
        times -= 1
    download_url(html_tail)
